package com.lemon.smartsafety.ui.adapter.safe.data;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.entity.common.SelectDataI;
import com.lemon.smartsafety.entity.event.datacoll.BuildingGroupDeleteEvent;
import com.lemon.smartsafety.listener.RecyclerTClickListener;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bingoogolapple.badgeview.BGABadgeTextView;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/29.
 */
public class BuildingGroupAdapter extends LemonRcvAdapter<SelectDataI> {
    private RecyclerTClickListener<SelectDataI> mListener;

    public BuildingGroupAdapter(List<SelectDataI> data, RecyclerTClickListener<SelectDataI> listener) {
        super(data);
        this.mListener = listener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new BuildingGroupItem();
    }

    public class BuildingGroupItem implements AdapterItem<SelectDataI> {


        @Bind(R.id.itemText)
        BGABadgeTextView itemText;
        @Bind(R.id.iv_delete)
        ImageView        ivDelete;
        @Bind(R.id.ripple)
        LinearLayout ripple;
        private SelectDataI mData;
        private int         mPosition;


        @Override
        public int getLayoutResId() {
            return R.layout.item_group_manager;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });
        /*    ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });*/
            ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BuildingGroupDeleteEvent event = new BuildingGroupDeleteEvent();
                    event.setTag(BuildingGroupDeleteEvent.class.getName());
                    event.setData(mData);
                    AppService.getBus().post(event);
                }
            });
        }

        @Override
        public void handleData(SelectDataI data, int position) {
            mData = data;
            mPosition = position;
            itemText.setText(data.getTitle());
        }
    }
}
